package com.prp.gwt.client.widgets.mail;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.vo.MailBoxList;
import com.prp.gwt.client.vo.MailboxInfo;

public class OutboxMailListPanel extends VerticalPanel {
	private static OutboxMailListPanel instance;
	private MailBoxList mailboxList;
	public static final int NUM_PER_PAGE = 20;
	private int index;
	HTML head = new HTML("<a>首页</a>");
	HTML next = new HTML("<a>下一页</a>");
	HTML last = new HTML("<a>上一页</a>");
	HTML tail = new HTML("<a>尾页</a>");

	public static OutboxMailListPanel getInstance() {
		if (instance == null)
			instance = new OutboxMailListPanel();
		return instance;
	}
	
	public void delete(MailboxInfo mailboxinfo) {
		mailboxList.deleteMailbox(mailboxinfo);
		clear();
		if (mailboxList.size() > 0) {
			toPage(index);
		} else {
			add(new HTML("您的发件箱里暂时没有邮件！"));
		}
	}

	private OutboxMailListPanel() {
		head.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(1);
			}
		});
		tail.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(getPageCount());
			}
		});
		next.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(index + 1);
			}
		});
		last.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(index - 1);
			}
		});
	}

	public void load(MailBoxList mailboxList) {
		this.mailboxList = mailboxList;
		clear();
		if (mailboxList.size() > 0) {
			toPage(1);
		} else {
			add(new HTML("您的发件箱里暂时没有邮件！"));
		}
	}

	public void toPage(int index) {
		this.index = index;
		clear();
		Window.setTitle("发件箱");

		loadPagePanel();
		loadFriendItem();
	}

	private void loadFriendItem() {
		int start = (index - 1) * NUM_PER_PAGE;
		int end = Math.min(index * NUM_PER_PAGE, mailboxList.size());
		for (int i = start; i < end; i++)
			add(new OutMailboxItem(mailboxList.get(i)));
	}

	/**
	 * to修改
	 * 
	 */
	private void loadPagePanel() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(5);
		int start = 0;
		int end = 0;
		if (getPageCount() <= 5 || index < 3) {
			start = 1;
			end = Math.min(5, getPageCount());
		} else if (getPageCount() - index < 3) {
			start = getPageCount() - 4;
			end = getPageCount();
		} else {
			start = index - 2;
			end = index + 2;
		}
		if (index != 1) {
			panel.add(head);
			panel.add(last);
		}
		for (int i = start; i <= end; i++) {
			if (i == index) {
				panel.add(new HTML(String.valueOf(i)));
				continue;
			}
			HTML label = new HTML("<a>" + String.valueOf(i) + "</a>");
			panel.add(label);
			final int j = i;
			label.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					toPage(j);
				}
			});
		}
		if (index != getPageCount()) {
			panel.add(next);
			panel.add(tail);
		}
		add(panel);
		setCellHorizontalAlignment(panel, ALIGN_RIGHT);
	}

	public int getPageCount() {
		return (mailboxList.size() - 1) / NUM_PER_PAGE + 1;
	}
}
